<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="提醒时间" prop="createTime">
          <el-date-picker
              v-model="queryParams.createTime"
              type="daterange"
              class="!w-[250px]"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-tree-select
              v-model="queryParams.deptId"
              placeholder="全部"
              :data="deptOptions"
              filterable
              clearable
              check-strictly
              :render-after-expand="false"
              style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
              v-model="queryParams.userName"
              placeholder="用户名称"
              clearable
              @keyup.enter="handleQuery()"
              style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="文件标识" prop="fileName">
          <el-input
              v-model="queryParams.fileName"
              placeholder="文件标识"
              clearable
              @keyup.enter="handleQuery()"
              style="width: 250px"
          />
        </el-form-item>
        <el-form-item label="下载标识" prop="isDownload">
          <el-select v-model="queryParams.isDownload" placeholder="全部" clearable class="!w-[150px]">
            <el-option label="已下载" :value="1"/>
            <el-option label="未下载" :value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search/>
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh/>
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
<!--      <div class="mb-10px">-->
<!--        <el-button-->
<!--            v-hasPerm="['system:lpsOrderDownload:add']"-->
<!--            type="success"-->
<!--            @click="handleOpenDialog()"-->
<!--        >-->
<!--          <template #icon>-->
<!--            <Plus/>-->
<!--          </template>-->
<!--          新增-->
<!--        </el-button>-->
<!--        <el-button-->
<!--            v-hasPerm="['system:lpsOrderDownload:delete']"-->
<!--            type="danger"-->
<!--            :disabled="removeIds.length === 0"-->
<!--            @click="handleDelete()"-->
<!--        >-->
<!--          <template #icon>-->
<!--            <Delete/>-->
<!--          </template>-->
<!--          删除-->
<!--        </el-button>-->
<!--      </div>-->

      <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="pageData"
          highlight-current-row
          border
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="index" label="序号" width="70" align="center"/>
        <el-table-column
            key="remindTime"
            label="提醒时间"
            prop="remindTime"
            width="200"
            align="center"
        />
        <el-table-column label="下载标识" width="150" align="center">
          <template #default="scope">
            <DictLabel v-model="scope.row.isDownload" code="is_download"/>
          </template>
        </el-table-column>
        <el-table-column
            key="downloadTime"
            label="下载时间"
            prop="downloadTime"
            width="200"
            align="center"
        />
        <!--        <el-table-column-->
        <!--            key="orderId"-->
        <!--            label="订单id"-->
        <!--            prop="orderId"-->
        <!--            min-width="150"-->
        <!--            align="center"-->
        <!--        />-->
        <el-table-column
            key="fileName"
            label="文件标识"
            prop="fileName"
            min-width="350"
            align="center"
            :show-overflow-tooltip="true"
        />
        <!--        <el-table-column-->
        <!--            key="userId"-->
        <!--            label="用户id"-->
        <!--            prop="userId"-->
        <!--            min-width="150"-->
        <!--            align="center"-->
        <!--        />-->
        <el-table-column
            key="userName"
            label="用户名称"
            prop="userName"
            width="200"
            align="center"
            :show-overflow-tooltip="true"
        />
        <!--        <el-table-column-->
        <!--            key="deptId"-->
        <!--            label="部门id"-->
        <!--            prop="deptId"-->
        <!--            min-width="150"-->
        <!--            align="center"-->
        <!--        />-->
        <el-table-column
            key="deptName"
            label="部门名称"
            prop="deptName"
            min-width="200"
            align="center"
            :show-overflow-tooltip="true"
        />

        <!--        <el-table-column-->
        <!--            key="remark"-->
        <!--            label="备注"-->
        <!--            prop="remark"-->
        <!--            min-width="200"-->
        <!--            align="center"-->
        <!--            :show-overflow-tooltip="true"-->
        <!--        />-->
<!--        <el-table-column fixed="right" label="操作" width="100" align="center">-->
<!--          <template #default="scope">-->
<!--            <el-button-->
<!--                v-hasPerm="['system:lpsOrderDownload:edit']"-->
<!--                type="primary"-->
<!--                size="small"-->
<!--                link-->
<!--                @click="handleOpenDialog(scope.row.id)"-->
<!--            >-->
<!--              <template #icon>-->
<!--                <Edit/>-->
<!--              </template>-->
<!--              编辑-->
<!--            </el-button>-->
<!--            <el-button-->
<!--                v-hasPerm="['system:lpsOrderDownload:delete']"-->
<!--                type="danger"-->
<!--                size="small"-->
<!--                link-->
<!--                @click="handleDelete(scope.row.id)"-->
<!--            >-->
<!--              <template #icon>-->
<!--                <Delete/>-->
<!--              </template>-->
<!--              删除-->
<!--            </el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->
      </el-table>

      <pagination
          v-if="total > 0"
          v-model:total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="handleQuery()"
      />
    </el-card>

    <!-- 下载记录表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="订单id" prop="orderId">
          <el-input
              v-model="formData.orderId"
              placeholder="订单id"
          />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input
              v-model="formData.userId"
              placeholder="用户id"
          />
        </el-form-item>

        <el-form-item label="用户名称" prop="userName">
          <el-input
              v-model="formData.userName"
              placeholder="用户名称"
          />
        </el-form-item>

        <el-form-item label="部门id" prop="deptId">
          <el-input
              v-model="formData.deptId"
              placeholder="部门id"
          />
        </el-form-item>

        <el-form-item label="部门名称" prop="deptName">
          <el-input
              v-model="formData.deptName"
              placeholder="部门名称"
          />
        </el-form-item>

        <el-form-item label="文件名称" prop="fileName">
          <el-input
              v-model="formData.fileName"
              placeholder="文件名称"
          />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
              v-model="formData.remark"
              placeholder="备注"
          />
        </el-form-item>

        <el-form-item label="下载标识(0-未下载 1-已下载)" prop="isDownload">
          <dict v-model="formData.isDownload" code="is_download"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import DeptAPI from "@/api/system/dept";

defineOptions({
  name: "LpsOrderDownload",
  inheritAttrs: false,
});

import LpsOrderDownloadAPI, {
  LpsOrderDownloadPageVO,
  LpsOrderDownloadForm,
  LpsOrderDownloadPageQuery
} from "@/api/system/lps-order-download";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const deptOptions = ref<OptionType[]>();
const total = ref(0);

const queryParams = reactive<LpsOrderDownloadPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 下载记录表格数据
const pageData = ref<LpsOrderDownloadPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 下载记录表单数据
const formData = reactive<LpsOrderDownloadForm>({});

// 下载记录表单校验规则
const rules = reactive({
  orderId: [{required: true, message: "请输入订单id", trigger: "blur"}],
  userId: [{required: true, message: "请输入用户id", trigger: "blur"}],
  deptId: [{required: true, message: "请输入部门id", trigger: "blur"}],
});

/** 查询下载记录 */
function handleQuery() {
  loading.value = true;
  LpsOrderDownloadAPI.getPage1(queryParams)
      .then((data) => {
        pageData.value = data.list;
        total.value = data.total;
      })
      .finally(() => {
        loading.value = false;
      });
}

/** 重置下载记录查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开下载记录弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改下载记录";
    LpsOrderDownloadAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增下载记录";
  }
}

/** 提交下载记录表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        LpsOrderDownloadAPI.update(id, formData)
            .then(() => {
              ElMessage.success("修改成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      } else {
        LpsOrderDownloadAPI.add(formData)
            .then(() => {
              ElMessage.success("新增成功");
              handleCloseDialog();
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭下载记录弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除下载记录 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
      () => {
        loading.value = true;
        LpsOrderDownloadAPI.deleteByIds(ids)
            .then(() => {
              ElMessage.success("删除成功");
              handleResetQuery();
            })
            .finally(() => (loading.value = false));
      },
      () => {
        ElMessage.info("已取消删除");
      }
  );
}

if (onMounted) {
  onMounted(async () => {
    // 加载区域下拉数据源
    deptOptions.value = await DeptAPI.getOptions();
    handleQuery();
  });
}
</script>

<style scoped>
:deep(.el-input-number .el-input__inner) {
  text-align: left !important;
}

:deep(.el-popper) {
  max-width: 500px;
}
</style>
